/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View.seguridad;

import Model.Usuario;
import Presenter.EnvioPedidoPresenter;
import Presenter.UsuarioPresenter;
import Principals.Main;
import Principals.Validadores;
import javax.swing.JOptionPane;

/**
 *
 * @author walter
 */
public class Frm_CambiarContrasenha extends javax.swing.JDesktopPane {
    Validadores val;
    Usuario usr = Main.usr;

    public Frm_CambiarContrasenha() {
        EnvioPedidoPresenter.actualizarEnvioPedido() ;
        initComponents();
//        val.desactivarAccionPegar(pwdNuevaContrasenha);
//        val.desactivarAccionPegar(pwdAntiguaContrasenha);
//        val.desactivarAccionPegar(pwdRepetirContrasenha);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlCambiarContrasenha = new javax.swing.JPanel();
        lblNuevaContrasenha = new javax.swing.JLabel();
        lblRepetirContrasenha = new javax.swing.JLabel();
        pwdNuevaContrasenha = new javax.swing.JPasswordField();
        pwdRepetirContrasenha = new javax.swing.JPasswordField();
        btnAceptar = new javax.swing.JButton();
        pwdAntiguaContrasenha = new javax.swing.JPasswordField();
        lblNuevaContrasenha1 = new javax.swing.JLabel();
        btnCancelar = new javax.swing.JButton();

        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        pnlCambiarContrasenha.setBorder(javax.swing.BorderFactory.createTitledBorder("Cambiar Contraseña"));

        lblNuevaContrasenha.setText("Nueva contraseña:");

        lblRepetirContrasenha.setText("Repetir contraseña:");

        pwdNuevaContrasenha.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                pwdNuevaContrasenhaKeyTyped(evt);
            }
        });

        pwdRepetirContrasenha.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                pwdRepetirContrasenhaKeyTyped(evt);
            }
        });

        btnAceptar.setText("Aceptar");
        btnAceptar.setMaximumSize(new java.awt.Dimension(75, 23));
        btnAceptar.setMinimumSize(new java.awt.Dimension(80, 30));
        btnAceptar.setPreferredSize(new java.awt.Dimension(80, 30));
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        pwdAntiguaContrasenha.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                pwdAntiguaContrasenhaKeyTyped(evt);
            }
        });

        lblNuevaContrasenha1.setText("Actual contraseña:");

        btnCancelar.setText("Cancelar");
        btnCancelar.setMinimumSize(new java.awt.Dimension(80, 30));
        btnCancelar.setPreferredSize(new java.awt.Dimension(80, 30));
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlCambiarContrasenhaLayout = new javax.swing.GroupLayout(pnlCambiarContrasenha);
        pnlCambiarContrasenha.setLayout(pnlCambiarContrasenhaLayout);
        pnlCambiarContrasenhaLayout.setHorizontalGroup(
            pnlCambiarContrasenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCambiarContrasenhaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlCambiarContrasenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlCambiarContrasenhaLayout.createSequentialGroup()
                        .addGroup(pnlCambiarContrasenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblRepetirContrasenha)
                            .addComponent(lblNuevaContrasenha))
                        .addGap(13, 13, 13)
                        .addGroup(pnlCambiarContrasenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(pwdNuevaContrasenha)
                            .addComponent(pwdRepetirContrasenha)))
                    .addGroup(pnlCambiarContrasenhaLayout.createSequentialGroup()
                        .addComponent(lblNuevaContrasenha1)
                        .addGap(18, 18, 18)
                        .addComponent(pwdAntiguaContrasenha)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlCambiarContrasenhaLayout.createSequentialGroup()
                .addContainerGap(65, Short.MAX_VALUE)
                .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(62, 62, 62))
        );
        pnlCambiarContrasenhaLayout.setVerticalGroup(
            pnlCambiarContrasenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCambiarContrasenhaLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnlCambiarContrasenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(pwdAntiguaContrasenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNuevaContrasenha1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlCambiarContrasenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(pwdNuevaContrasenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNuevaContrasenha))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlCambiarContrasenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(pwdRepetirContrasenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblRepetirContrasenha))
                .addGap(18, 18, 18)
                .addGroup(pnlCambiarContrasenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlCambiarContrasenha, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlCambiarContrasenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void limpiar() {
        pwdAntiguaContrasenha.setText("");
        pwdNuevaContrasenha.setText("");
        pwdRepetirContrasenha.setText("");
    }

    private boolean validaContrasenhaHistorica(String pass) {
        return UsuarioPresenter.validaContrasenhaHistorica(pass, usr);
    }

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        String pwdAntigua = new String(pwdAntiguaContrasenha.getPassword());
        String pwdNuevo = new String(pwdNuevaContrasenha.getPassword());
        String pwdRepetir = new String(pwdRepetirContrasenha.getPassword());

        if (!"".equals(pwdAntigua) && !"".equals(pwdNuevo) && !"".equals(pwdRepetir)) {
            if (UsuarioPresenter.login(usr.getUsuario(), pwdAntigua) != null) {
                if (pwdNuevo.equals(pwdRepetir)) {
                    if (!pwdNuevo.equals(pwdAntigua)) {
                        if (validaContrasenhaHistorica(pwdNuevo)) {
                            UsuarioPresenter.cambiarContrasenha(pwdNuevo);
                            JOptionPane.showMessageDialog(this, "Se cambió la contraseña correctamente");
                            limpiar();
                        } else {
                            JOptionPane.showMessageDialog(this, "Debe usar una contraseña diferente a las últimas 3");
                            limpiar();
                        }
                    } else {
                        JOptionPane.showMessageDialog(this, "No se puede usar la misma contraseña que la actual");
                        limpiar();
                    }
                } else {
                    JOptionPane.showMessageDialog(this, "Las nuevas contraseñas no coinciden");
                    limpiar();
                }
            } else {
                JOptionPane.showMessageDialog(this, "Contraseña actual incorrecta");
                limpiar();
            }
        } else {
            JOptionPane.showMessageDialog(this, "Por favor ingrese todos los campos");
            limpiar();
        }
    }//GEN-LAST:event_btnAceptarActionPerformed

    private void pwdAntiguaContrasenhaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_pwdAntiguaContrasenhaKeyTyped
        if (pwdAntiguaContrasenha.getPassword().length >= 8) {
            evt.consume();
        }
    }//GEN-LAST:event_pwdAntiguaContrasenhaKeyTyped

    private void pwdNuevaContrasenhaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_pwdNuevaContrasenhaKeyTyped
        if (pwdNuevaContrasenha.getPassword().length >= 8){
            evt.consume();
        }
    }//GEN-LAST:event_pwdNuevaContrasenhaKeyTyped

    private void pwdRepetirContrasenhaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_pwdRepetirContrasenhaKeyTyped
        if (pwdRepetirContrasenha.getPassword().length >= 8){
            evt.consume();
        }
    }//GEN-LAST:event_pwdRepetirContrasenhaKeyTyped

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JLabel lblNuevaContrasenha;
    private javax.swing.JLabel lblNuevaContrasenha1;
    private javax.swing.JLabel lblRepetirContrasenha;
    private javax.swing.JPanel pnlCambiarContrasenha;
    private javax.swing.JPasswordField pwdAntiguaContrasenha;
    private javax.swing.JPasswordField pwdNuevaContrasenha;
    private javax.swing.JPasswordField pwdRepetirContrasenha;
    // End of variables declaration//GEN-END:variables
}
